package com.yanqu.road.dao.impl.activity.biaoju;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuImpl extends TempDataObjectDao<CrossBiaoJuData> {

    public static String getCrossBiaoJuDataSql = "select * from `t_c_biaoju` where `activityId`=? and unionGroup=?";
    public CrossBiaoJuData getCrossBiaoJuData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossBiaoJuDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossBiaoJuDataSql = "update `t_c_biaoju` set nowRound=?,settleRound=?,bestQualityList=?,historyLevelAmountMap=?,historyCalcResultMap=? where activityId=? and unionGroup=?";
    @Override
    protected boolean updateData(CrossBiaoJuData crossBiaoJuData) {
        crossBiaoJuData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getNowRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getSettleRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuData.getBestQualityList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuData.getHistoryLevelAmountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuData.getHistoryCalcResultMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getUnionGroup()));
        if (getDb().executeNoneQuery(updateCrossBiaoJuDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossBiaoJuDataSql = "insert into `t_c_biaoju`(activityId,unionGroup,nowRound,settleRound,bestQualityList,historyLevelAmountMap,historyCalcResultMap) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossBiaoJuData crossBiaoJuData) {
        crossBiaoJuData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getNowRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuData.getSettleRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuData.getBestQualityList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuData.getHistoryLevelAmountMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuData.getHistoryCalcResultMap())));
        if (getDb().executeNoneQuery(insertCrossBiaoJuDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossBiaoJuData parseData(ResultSet rs) throws SQLException {
        CrossBiaoJuData crossBiaoJuData = new CrossBiaoJuData();
        crossBiaoJuData.setActivityId(rs.getInt("activityId"));
        crossBiaoJuData.setUnionGroup(rs.getInt("unionGroup"));
        crossBiaoJuData.setNowRound(rs.getInt("nowRound"));
        crossBiaoJuData.setSettleRound(rs.getInt("settleRound"));
        crossBiaoJuData.setBestQualityList(JSON.parseObject(rs.getString("bestQualityList"), new TypeReference<List<Integer>>() {}));
        crossBiaoJuData.setHistoryLevelAmountMap(JSON.parseObject(rs.getString("historyLevelAmountMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.util.Map<java.lang.Integer, java.lang.Integer>>>() {}));
        crossBiaoJuData.setHistoryCalcResultMap(JSON.parseObject(rs.getString("historyCalcResultMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.util.Map<java.lang.Integer, java.math.BigDecimal>>>() {}));

        return crossBiaoJuData;
    }
}
